/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.tnmk.worktime.gui.widget;

import java.awt.Component;
import java.awt.Container;
import java.awt.Frame;
import javax.swing.Popup;
import javax.swing.PopupFactory;
import org.tnmk.worktime.gui.utils.EffectUtils;

/**
 * The inside panel will be handled (shown/hidden) as a pop-up
 * @author tnmkhoi
 */
public class PanelPopup implements OpenableHandler {

  private final Frame parent;
  private final Container panel;
  private Popup popup;
  private int positionX;
  private int positionY;

  public PanelPopup(Frame parent, Container panel) {
    this.parent = parent;
    this.panel = panel;
    if (this.panel instanceof PopupPanel) {
      PopupPanel popupPanel = (PopupPanel)this.panel;
      popupPanel.setPopup(this);
    }
  }

  @Override
  public void setLocation(int x, int y) {
    this.positionX = x;
    this.positionY = y;
  }

  @Override
  public void show() {
    //hide current popup (if opening)
    hide();

    //create the new popup and show it.
    PopupFactory factory = PopupFactory.getSharedInstance();
    popup = factory.getPopup(parent, panel, positionX, positionY);
    popup.show();
    EffectUtils.fadeOn(panel, 50);
  }

  @Override
  public void hide() {
    if (popup != null) {
      popup.hide();
    }
  }

  @Override
  public Component getComponent() {
    return this.panel;
  }
}
